﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DTO;
using AutoMapper;
using System.Data;

namespace Data
{
   
    public class TacGiaRepository
    {
        private static TacGiaDTO MapTacGiaEFToTacGiaDTO(TACGIA tacgiaEF)
        {
            Mapper.CreateMap<TACGIA, TacGiaDTO>();
            TacGiaDTO tacgiaDTO = Mapper.Map<TACGIA, TacGiaDTO>(tacgiaEF);
            return tacgiaDTO;
        }
        private static TACGIA MapTacGiaDTOToTacGiaEF(TacGiaDTO tacgiaDTO)
        {
            Mapper.CreateMap<TacGiaDTO, TACGIA>();
            TACGIA tacgiaEF = Mapper.Map<TacGiaDTO, TACGIA>(tacgiaDTO);
            return tacgiaEF;
        }
        public static TacGiaDTO GetTacGiaByID(int matacgia)
        {
            TacGiaDTO tacgiaDTO = new TacGiaDTO();
            using (QuanLyNhaSachEntities context = new QuanLyNhaSachEntities())
            {
                try
                {
                    TACGIA tacgiaEF = (from tacgia in context.TACGIAList
                                       where tacgia.MaTacGia == matacgia
                                       select tacgia).First();
                    tacgiaDTO = MapTacGiaEFToTacGiaDTO(tacgiaEF);

                }
                catch
                {
                }
            }
            return tacgiaDTO;

        }
        public static List<TacGiaDTO> GetAllTacGia()
        {
            List<TacGiaDTO> list = new List<TacGiaDTO>();
            try
            {
                using (QuanLyNhaSachEntities context = new QuanLyNhaSachEntities())
                {

                    var query = from tacgia in context.TACGIAList
                                select tacgia;
                    foreach (var tacgiaEF in query)
                    {
                        TacGiaDTO tacgiaDTO = MapTacGiaEFToTacGiaDTO(tacgiaEF);
                        list.Add(tacgiaDTO);
                    }
                }
            }
            catch
            {
            }
            return list;

        }
        public static void Insert(TacGiaDTO tacgiaDTO)
        {
            using (QuanLyNhaSachEntities context = new QuanLyNhaSachEntities())
            {
                
                try
                {
                    TACGIA tacgiaEF = new TACGIA();
                    tacgiaEF = MapTacGiaDTOToTacGiaEF(tacgiaDTO);
                    context.AddToTACGIAList(tacgiaEF);
                    context.SaveChanges();
                }
                catch(Exception)
                {
                }
            }
        }
        public static void Update(TacGiaDTO tacgiaDTO)
        {
            using (QuanLyNhaSachEntities context = new QuanLyNhaSachEntities())
            {
               
                try
                {
                    EntityKey key = context.CreateEntityKey("TACGIAList", new TACGIA { MaTacGia = tacgiaDTO.MaTacGia });
                    TACGIA origtacgia = context.GetObjectByKey(key) as TACGIA;
                    context.Detach(origtacgia);
                    context.Attach(origtacgia);
                    TACGIA updateTacGia = MapTacGiaDTOToTacGiaEF(tacgiaDTO);
                    context.ApplyCurrentValues<TACGIA>("TACGIAList", updateTacGia);
                    context.SaveChanges();
                }
                catch(Exception)
                {

                }

            }

        }
    }
}
